package com.eqdd.yiqidian.ui;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RegistActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.mine.qiye.QiyeRegisterActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.utils.ValidatorUtil;
import com.eqdd.yiqidian.widget.CountDownButtonHelper;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GetPwdSendCheckCodeActivity extends BaseActivity {
    private RegistActivityCustom dataBinding;
    private AlertDialog alertDialog;
    private String msgid;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_regist);
        initTopTitleBar(View.VISIBLE, "忘记密码");
        dataBinding.tvInstruction.setVisibility(View.GONE);
//        init();
    }

    @Override
    public void initData() {

    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_yanzheng:
                String phone = dataBinding.etPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(phone) && ValidatorUtil.isMobile(phone)) {
                    CountDownButtonHelper helper = new CountDownButtonHelper(
                            dataBinding.tvYanzheng, "倒计时", 60, 1);
                    helper.setOnFinishListener(() -> {
                        dataBinding.tvYanzheng.setText("获取验证码");
                        dataBinding.tvYanzheng.setEnabled(true);
                    });
                    helper.start();
                    dataBinding.tvYanzheng.setEnabled(false);
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.SEND_SMS)
                            .params("tel", phone)
                            .execute(new DialogCallBack<HttpResult>(GetPwdSendCheckCodeActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    if (httpResult.getStatus() == 200) {
                                        ToastUtil.showShort("发送验证码成功");
                                        try {
                                            JSONObject jsonObject = new JSONObject(httpResult.getMsg());
                                            msgid = jsonObject.getString("msg_id");
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                        System.out.println(msgid);
                                    }
                                }
                            });
                } else {
                    ToastUtil.showShort("请输入有效的手机号");
                }
                break;
            case R.id.tv_regeist:
                String telphone = dataBinding.etPhone.getText().toString().trim();
                String yzm = dataBinding.etYanzheng.getText().toString().trim();
                if (!TextUtils.isEmpty(telphone) && ValidatorUtil.isMobile(telphone)) {
                    if (!TextUtils.isEmpty(yzm) && yzm.length() == 6) {
                        Bundle bundle = new Bundle();
                        bundle.putString("smsCode", yzm);
                        bundle.putString("phone", telphone);
                        bundle.putString("msgid", msgid);
                        startActivityForResult(PhonePasswordGetActivity.class, bundle, Config.PHONE_PWD_GET);
                    } else {
                        ToastUtil.showShort("验证码错误");
                    }
                } else {
                    ToastUtil.showShort("请输入有效的手机号");
                }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.PHONE_PWD_GET && resultCode == Config.SUCCESS) {
            finish();
        }
    }
    //
//    // 获取短信信息
//    private void init() {
//        try {
//            EventHandler eventHandler = new EventHandler() {
//                public void afterEvent(int event, int result, Object data) {
//                    System.out.println(event + "===" + result + "===" + data.toString() + "register");
//                    final Message msg = new Message();
//                    msg.arg1 = event;
//                    msg.arg2 = result;
//                    msg.obj = data;
//                    Observable.just(msg)
//                            .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
//                            .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
//                            .subscribe(new Observer<Message>() {
//                                @Override
//                                public void onNext(Message msg) {
//                                    handleMsg(msg);
//                                }
//
//                                @Override
//                                public void onCompleted() {
//                                }
//
//                                @Override
//                                public void onError(Throwable e) {
//                                }
//                            });
//                }
//            };
//            SMSSDK.registerEventHandler(eventHandler); // 注册短信回调
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }

//    public boolean handleMsg(Message msg) {
//        int event = msg.arg1;
//        int result = msg.arg2;
//        if (result == SMSSDK.RESULT_COMPLETE) {
//            if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//验证码验证成功
//                String telphone = dataBinding.etPhone.getText().toString().trim();
//                Bundle bundle = new Bundle();
//                bundle.putString("phone", telphone);
//                switch (mode) {
//                    case Config.MODE_REGISTER:
//                        startActivity(RegistTwo.class, bundle);
//                        finish();
//                        break;
//                    case Config.MODE_REGISTER_QIYE:
//                        new HttpPresneter.Builder<String>(HttpConfig.IS_CAN_REGISTER_COM, String.class, this)
//                                .Params("uid", telphone)
//                                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
//                                    @Override
//                                    public void onError(Throwable e) {
//                                    }
//
//                                    @Override
//                                    public void onNext(Object o) {
//                                        BaseBean baseBean = GsonUtils.changeGsonToBean((String) o, BaseBean.class);
//                                        ToastUtil.showShort(baseBean.getMsg());
//                                        if (baseBean.getStatus() == 200) {
//                                            startActivity(QiyeRegisterActivity.class, bundle);
//                                            finish();
//                                        }
//                                    }
//                                })
//                                .build()
//                                .post();
//                        break;
//                    case Config.MODE_PASS_FORGET:
//                        startActivity(PhonePasswordGetActivity.class, bundle);
//                        finish();
//
//                        break;
//
//                }
//            } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {//获取验证码成功
//                ToastUtil.showLong("获取验证码成功");
//            } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//            }
//        } else {
//            ToastUtil.showLong("验证码出错");
//        }
//        return false;
//    }

    @Override
    protected void onDestroy() {
        // 销毁回调监听接口
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }
}
